#pragma once
#include "../../../include/log_interface.hpp"
#include "../../../include/lock.hpp"
#include <stdarg.h>
#include <stdlib.h>
#include <direct.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>

#ifdef _WIN32
#define PATH_MAX MAX_PATH
#endif

#define MAX_LOGFILE_SIZE    (1024*1024*1024)
#define MAX_LOG_FILE_NAME   128

#include "logthread.h"

class CFileLog :
    public IFileLog
{
public:
    CFileLog(void);
    virtual ~CFileLog(void);

    bool Init(const char* pszDir, const char* pszLogFileName);
    void UnInit(void);

    virtual void OpenLevel(ELogLevel eLogLV);
    virtual void CloseLevel(ELogLevel eLogLV);
    virtual bool IsLevelOpen(ELogLevel eLogLV);
    virtual void Flush(void);

    virtual bool LogCri(const char* szFormat, ...);
    virtual bool LogWrn(const char* szFormat, ...);
    virtual bool LogInf(const char* szFormat, ...);
    virtual bool LogDbg(const char* szFormat, ...);
    //virtual bool Log(ELogLevel eLogLV, const char* szFormat, ...);

    virtual void Release(void);

    void Write(const char* pszLog, bool log_console);
    void FlushLog(void);

    inline bool need_flush(void){return m_need_flush;}
protected:
    bool __MkDir( const char* pszDir );
    void __CheckLog(void);

protected:
    char        m_log_buff[MAX_LOG_CACHE];
    char        m_szDir[PATH_MAX];
    char        m_szFileName[MAX_LOG_FILE_NAME];
    FILE*       m_pFile;
    INT32       m_index;
    UINT32      m_flag;
    CLogThread  m_log_thread;
    struct tm   m_Time;
    bool        m_need_flush;
    CThreadLock m_log_lock;
};

struct log_data_head 
{
    ELogLevel   log_level;
    INT32       data_len;
    CFileLog*   log;
    bool        log_console;
};
